
package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import model.Student;
import model.User;
import utils.DBconn;

public class UserDao {
	
	// 数据库连接
	
		
//——————————————————————————————————————————————功能1—————————————————————————————————————————————————————————————	
		// 用户个人信息查询，在gyuser中进行
		public List<User> getAlonePersonMessage(String id) {
		    List<User> personMessage = new ArrayList<>();
		    String sql = "SELECT * FROM gyuser WHERE id = ?";

		    try (Connection conn = DBconn.getConnection();
		         PreparedStatement stmt = conn.prepareStatement(sql)) {

		        stmt.setString(1, id);
		        try (ResultSet rs = stmt.executeQuery()) {
		            while (rs.next()) {
		                User pm = new User();
		                pm.setName(rs.getString("name"));
		                pm.setId(rs.getString("id"));
		                pm.setPassword(rs.getString("password"));
		                pm.setPower(rs.getString("power"));
		                personMessage.add(pm);
		            }
		        }
		    } catch (SQLException e) {
		        // 记录日志或处理异常，例如：throw new RuntimeException("Failed to query user information", e);
		        e.printStackTrace();
		    }
		    return personMessage;
		}
		    
		    
//———————————————————————————————————————————————功能2————————————————————————————————————————————————————————————
		// 更改密码，在gyuser中进行
		// 更改用户密码
		 public boolean updatePassword(String id, String newEncryptedPassword) {
		        String sql = "UPDATE gyuser SET password = ? WHERE id =?";
		 
		        try (Connection conn = DBconn.getConnection();
		             PreparedStatement stmt = conn.prepareStatement(sql)) {
		 
		            // 设置参数值
		            stmt.setString(1, newEncryptedPassword);
		            stmt.setString(2, id);
		 
		            // 执行更新操作
		            int rowsAffected = stmt.executeUpdate();
		 
		            // 根据受影响的行数返回结果
		            return rowsAffected > 0;
		 
		        } catch (SQLException e) {
		            e.printStackTrace();
		            // 可以选择将异常信息记录到日志中，而不是仅仅打印堆栈跟踪
		            // 这里简单地返回false表示更新失败
		            return false;
		        }
		    }
	 
//———————————————————————————————————————————————功能3————————————————————————————————————————————————————————————
//改动学生信息
		//增加学生信息
		 public boolean addStu(Student stu) {
//				测试行
			       System.out.println("addStu:"+stu.getId()+"name:"+stu.getName()+"class:"+stu.getClassName()+" roomid:"+stu.getRoomid());

				// TODO Auto-generated method stub
				  String sql = "INSERT INTO student (id, name,class,roomid) VALUES (?, ?, ?, ?)";
			        try (Connection conn = DBconn.getConnection();
			             PreparedStatement stmt = conn.prepareStatement(sql)) {
			            stmt.setString(1, stu.getId());
			            stmt.setString(2, stu.getName());
			            stmt.setString(3, stu.getClassName());
			            stmt.setString(4, stu.getRoomid());
			      
			            return stmt.executeUpdate() > 0;
			        } catch (SQLException e) {
			            e.printStackTrace();
			        }
			        return false;
			}

			
			
//			更新学生信息
		    public boolean updateStu(Student stu,String oldid) {
		        String sql = "UPDATE student SET id = ? ,name = ? ,class = ?,roomid = ?, WHERE id = ? ";
		        try (Connection conn = DBconn.getConnection();
		             PreparedStatement stmt = conn.prepareStatement(sql)) {
		            
		        	 stmt.setString(1, stu.getId());
		             stmt.setString(2, stu.getName());
		             stmt.setString(3, stu.getClassName());
		             stmt.setString(4, stu.getRoomid());
		             
		             stmt.setString(5, oldid);
		        
		             
		            return stmt.executeUpdate() > 0;
		        } catch (SQLException e) {
		            e.printStackTrace();
		        }
		        return false;
		    }
		    
		//删除学生信息
		    public boolean deleteStu(String id) {
				// TODO Auto-generated method stub
		    	 String sql = "DELETE FROM Student WHERE id = ? ";
		         try (Connection conn = DBconn.getConnection();
		              PreparedStatement stmt = conn.prepareStatement(sql)) {
		             stmt.setString(1, id);
		        
		             return stmt.executeUpdate() > 0;
		         } catch (SQLException e) {
		             e.printStackTrace();
		         }
		         return false;
		    }
//		    查找用户是否存在
		    public boolean checkStudentIDExists(String id) {
				// TODO Auto-generated method stub
		    	boolean exists = false;
		        try (Connection conn = DBconn.getConnection()) {
		            String sql = "SELECT COUNT(*) FROM student WHERE id = ?";
		            try (PreparedStatement stmt = conn.prepareStatement(sql)) {
		                stmt.setString(1, id);
		                ResultSet rs = stmt.executeQuery();
		                if (rs.next() && rs.getInt(1) > 0) {
		                    exists = true;
		                }
		            }
		        } catch (SQLException e) {
		            e.printStackTrace();
		        }
		        return exists;
			}
		 
		}

		
	


